home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- * scalable.c:
- *
- * This file contains a few simple examples of using scalable fonts
- * which were new to X11R5. There are now two types of fonts: bitmap
- * fonts and "derived instances of scalable fonts". Note that for a
- * particular font specification it's not possible to determine whether
- * the font actually displayed is a bitmap font or a derived instance of
- * a scalable font.
- *
- * In a list of fonts, the name string of a scalable font will have a `0'
- * in the PIXEL_SIZE, POINT_SIZE, and AVERAGE_WIDTH fields.
- *
- * When you specify a font name with wildcards, for scalable fonts, you
- * must provide a "well-formed" name. A "well-formed" name is one in
- * which you specify all 14 hyphens in the name string. To choose a
- * scalable font, put `0' in the PIXEL_SIZE, POINT_SIZE, and
- * AVERAGE_WIDTH fields. For example,
- *
- * "-*-helvetica-medium-r-*-*-0-0-*-*-*-0-*-*"
- *
- * is a "well-formed" name. A name that doesn't contain all 14 hyphens,
- * like:
- *
- * "-*-helvetica-medium-r-*"
- *
- * is not.
- *
- * The source file includes an example of using scalable fonts, and some
- * utility functions to help select a scalable font, determine whether a
- * font specification is "well-formed", and a few others.
- *
- * See the comments in the source for more information.
- *
- * See also, chapter 2 of the O'Reilly book: "Programmer's Supplement
- * for Release 5 of the X Window System, Version 11", by David Flanagan.
- *
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
-
- static Bool IsScalableFont( const char * );
- static XFontStruct *LoadQueryScalableFont( Display *, const int,
- const char *, const unsigned int );
- static void FontQueryExamples( Display * );
- static void drawWindow( Display *, Window, GC,
- XFontStruct *[], const unsigned int, const unsigned int );
-
- main(void)
- {
- Display *dpy;
- Window wind, rwin;
- int scr;
- unsigned int keep_going = 1;
- XEvent ev;
- unsigned int i;
- const unsigned int MAXFONTS = 32;
- XFontStruct *courier_s[MAXFONTS];
- GC gc;
-
- XWMHints wmhints;
- XSizeHints wmsizehints;
- XClassHint classhint;
- XColor dsg, edsg, ylo, eylo, dbl, edbl;
-
- dpy = XOpenDisplay("");
- scr = DefaultScreen(dpy);
- rwin = RootWindow(dpy, scr);
-
-
- wind = XCreateSimpleWindow( dpy, rwin, 0, 0, 512, 512, 2,
- BlackPixel(dpy,scr), WhitePixel(dpy,scr));
-
- classhint.res_name = "LEFT or RIGHT Mouse to Change Font Size";
- classhint.res_class = "Scalable Fonts";
- XSetClassHint(dpy, wind, &classhint);
-
- wmhints.input = True;
- wmhints.flags = InputHint;
- XSetWMHints(dpy, wind, &wmhints);
-
- wmsizehints.x = 100;
- wmsizehints.y = 100;
- wmsizehints.width = 100;
- wmsizehints.height = 100;
- wmsizehints.flags = USPosition | USSize;
- XSetWMNormalHints(dpy, wind, &wmsizehints);
-
- gc = XCreateGC( dpy, wind, (Mask)0, NULL );
- XCopyGC( dpy, XDefaultGC(dpy,scr), (Mask)(~0) , gc );
-
- XAllocNamedColor( dpy, DefaultColormap(dpy,scr), "darkslategray",
- &dsg, &edsg );
-
- XAllocNamedColor( dpy, DefaultColormap(dpy,scr), "yellow", &ylo, &eylo );
-
- XAllocNamedColor( dpy, DefaultColormap(dpy,scr), "blue", &dbl, &edbl );
-
- XSetForeground( dpy, gc, ylo.pixel );
- XSetBackground( dpy, gc, dsg.pixel );
- XSetWindowBackground( dpy, wind, dsg.pixel );
-
- for( i = 0; i < MAXFONTS; i++ ){
- courier_s[i] = LoadQueryScalableFont( dpy, scr,
- "-*-helvetica-medium-r-*-*-0-0-*-*-*-0-*-*",
- 10*(i+6) );
- }
-
- XSelectInput( dpy, wind, ButtonPressMask|KeyPressMask|ExposureMask);
- XMapWindow( dpy, wind );
-
- do {
- XNextEvent( dpy, &ev );
- switch( ev.type )
- {
- case Expose:
- while( XCheckTypedEvent(dpy, Expose, &ev) );
- i = 0;
- drawWindow( dpy, wind, gc, courier_s, i, MAXFONTS );
- break;
-
- case KeyPress:
- XCloseDisplay(dpy);
- keep_going = 0;
- break;
-
- case ButtonPress:
- if( ev.xbutton.button == Button1 )
- i++;
- else if( ev.xbutton.button == Button3 )
- i--;
- else if( ev.xbutton.button == Button2 )
- i = 0;
- drawWindow( dpy, wind, gc, courier_s, i, MAXFONTS );
- break;
-
- default:
- break;
- }
- } while( keep_going );
-
- XCloseDisplay( dpy );
- exit(0);
- }
-
- static void drawWindow( Display *dpy, Window wind, GC gc,
- XFontStruct *courier_s[], const unsigned int i, const unsigned int MAXFONTS )
- {
- char cbuf[64];
- char lbuf[256];
-
- XClearWindow( dpy, wind );
- XSetFont( dpy, gc, courier_s[i%MAXFONTS]->fid );
- XDrawString( dpy, wind, gc, 10, 100, "The Quick Brown Fox", 19 );
- sprintf(cbuf, "%u", ((i%MAXFONTS)+6));
- strcpy(lbuf, "Point size is: ");
- strcat(lbuf, cbuf);
- XDrawString( dpy, wind, gc, 10, 200, lbuf, (int)strlen(lbuf) );
- }
-
-
- /* This routine returns True if the passed name is a well-formed XLFD style
- font name with a pixel size, point size, and average width
- (fields 7, 8, and 12) of "0". */
- static Bool IsScalableFont( const char *name )
- {
- unsigned int i, field;
-
- if( (name == NULL) || (name[0] != '-') ) return( False );
-
- for( i = field = 0; name[i] != '\0'; i++ ){
- if( name[i] == '-' ) {
- field++;
- if( (field == 7 ) || (field == 8) || (field == 12) )
- if( (name[i+1] != '\0') || (name[i+2] != '-') )
- return( False );
- }
- }
- if( field != 14 ) return( False );
- else return( True );
- }
-
- /* This routine is passed a scalable font name and a point size. It
- returns an XFontStruct for the given font scaled to the specified
- size and the exact resolution of the screen. The font name is assumed
- to be a well-formed XLFD name, and to have pixel size, point size,
- and average width fields of "0" and arbitrary x-resolution and y-resolution
- fields. Size is specified in tenths of points. Returns NULL if the
- name is malformed or no such font exists. */
-
- static XFontStruct *LoadQueryScalableFont( Display *dpy, const int screen,
- const char *name, const unsigned int size )
- {
- unsigned int i, j, field;
- char newname[500];
- unsigned int res_x, res_y; /* rez values for this screen */
-
- /* catch obvious errors */
- if( (name == NULL) || (name[0] != '-') ) return( NULL );
-
- /* calculate our screen rez in dpi. 25.4mm == 1 inch */
- res_x = (unsigned int)((float)DisplayWidth( dpy, screen )/( (float)DisplayWidthMM(dpy, screen)/(float)25.4));
- res_y = (unsigned int)((float)DisplayHeight( dpy, screen )/( (float)DisplayHeightMM(dpy, screen)/(float)25.4));
-
- /* copy the font name, changing the scalable fields as we do so */
- for( i = j = field = 0; name[i] != '\0' && field <= 14; i++ ) {
- newname[j++] = name[i];
- if( name[i] == '-' ) {
- field++;
- switch( field ) {
- case 7: /* pixel size */
- case 12: /* average width */
- /* change from "-0-" to "-*-" */
- newname[j] = '*';
- j++;
- if( name[i+1] != '\0' ) i++;
- break;
- case 8: /* point size */
- /* change from "-0-" to "-<size>-" */
- sprintf(&newname[j], "%d", size );
- while( newname[j] != '\0' ) j++;
- if( name[i+1] != '\0' ) i++;
- break;
- case 9: /* x-res */
- case 10: /* y-res */
- /* change from an unspec rez to res_x or res_y */
- sprintf(&newname[j], "%d", (field == 9) ? res_x : res_y );
- while( newname[j] != '\0' ) j++;
- while( (name[i+1] != '-') && (name[i+1] != '\0') ) i++;
- break;
- }
- }
- }
- newname[j] = '\0';
-
- /* if there aren't 14 hyphens, it isn't a well formed name */
- if( field != 14 ) return( NULL );
-
- return( XLoadQueryFont( dpy, newname ) );
- }
-
- static void FontQueryExamples( Display *dpy )
- {
- unsigned int i;
- unsigned int count = 0;
- Font font;
- char **fonts;
-
- /* List all Latin-1 fonts. Returned names of scalable fonts will have "0"
- * for pixel size, point size, and average width */
- fonts = XListFonts( dpy, "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1", 1000,
- (int *)&count );
- /*
- printf( "All Latin-1 fonts:\n" );
- for( i = 0; i < count; i++ )
- printf( "%s\n", fonts[i] );
- */
-
- /* List all scalable courier fonts. Non-scalable fonts will
- not be listed */
- printf( "\nAll scalable Courier fonts:\n" );
- fonts = XListFonts( dpy, "-*-courier-*-*-*-*-0-0-*-*-*-0-*-*", 200,
- (int *)&count );
- for( i = 0; i < count; i++ )
- printf( "%s\n", fonts[i] );
-
- /* Load a 12-point bold helvetica font defined at a 100x100 dpi
- resolution. The actual font loaded might be a derived instance
- of a scalable font, or it might be a bitmap font-- there is no way
- to distinguish between them. */
-
- font = XLoadFont( dpy,
- "-*-helvetica-bold-r-*-*-*-120-100-100-*-*-iso8859-1" );
-
- /* Load a 20 pixel high helvetica font defined at 100x100 dpi */
- font = XLoadFont( dpy,
- "-*-helvetica-medium-r-*-*-20-*-100-100-*-*-iso8859-1" );
- /* load all 13-point Latin-1 Helvetica fonts defined at a 106x7 dpi
- resolution. This pattern will match derived instances of scalable
- fonts, and will probably only match derived instances of scalable
- fonts, because there are not likely to be bitmap fonts defined at this
- particular size and resolution */
-
- printf( "\nAll 13-point Helvetica fonts at 106x97 resolution:\n" );
- fonts = XListFonts( dpy, "-*-helvetica-*-*-*-*-*-130-106-97-*-*-iso8859-1",
- 50, (int *)&count );
- for( i = 0; i < count; i++ )
- printf( "%s\n", fonts[i] );
-
- /* List 15-point bold oblique Helvetica fonts. Derived instances of
- scalable fonts will probably not be included in the list because
- the pattern does not have all 14 fields. */
-
- printf( "\nAll 15-point Helvetica bold oblique fonts:\n" );
- fonts = XListFonts( dpy, "-*-helvetica-bold-o-*-*-*-150-*", 50, (int *)&count );
- for( i = 0; i < count; i++ )
- printf( "%s\n", fonts[i] );
-
- /* List all 17-point, 17-pixel bold oblique Helvetica fonts defined
- at 100x100dpi. This pattern will not match any derived instances
- of scalable fonts (nor any font) because a 17 point font at 100dpi
- is not 17 pixels high. */
-
- printf( "\nAll 17-point Helvetica bold oblique fonts at 100x100 dpi:\n" );
- fonts = XListFonts( dpy,
- "-*-helvetica-bold-o-*-*-17-170-100-100-*-*-iso8859-1",
- 50, (int *)&count );
- for( i = 0; i < count; i++ )
- printf( "%s\n", fonts[i] );
- }
-